package com.zxkj.commonpatrol.ui;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.tech.MifareClassic;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.cunoraz.gifview.library.GifView;
import com.czy.basecommon.utils.base.BaseActivity;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.NFCTool;
import com.se.etf.view.JakeDate;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.R2;
import com.zxkj.commonpatrol.view.JakeTime;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Jake Han
 */
public class NfcActivity extends BaseActivity {

    @BindView(R2.id.jakeTime)
    JakeTime jakeTime;
    @BindView(R2.id.tb_return)
    RelativeLayout tbReturn;
    @BindView(R2.id.tb_title)
    TextView tbTitle;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.jakeDate)
    JakeDate jakeDate;
    @BindView(R2.id.gifView)
    GifView gifView;
    @BindView(R2.id.ll_nfc_before)
    LinearLayout llNfcBefore;
    @BindView(R2.id.ll_nfc_after)
    LinearLayout llNfcAfter;
    /**
     * NFC params
     */
    private NfcAdapter nfcAdapter;
    private PendingIntent mPendingIntent;
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private String PIN = "";
    private Typeface METROSANT;
    private Typeface tt0246m;

    /**
     * NFC params
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_nfc;
    }

    @Override
    protected void initUI() {
        initTypefaces();
        tbTitle.setText("NFC");
        tbTitle.setTypeface(METROSANT);
        jakeTime.setTypeface(tt0246m);
        jakeDate.setTypeface(tt0246m);
    }
    private void initTypefaces(){
        METROSANT = Typeface.createFromAsset(getAssets(),"METROSANT-REGULAR.OTF");
        tt0246m = Typeface.createFromAsset(getAssets(),"tt0246m_.ttf");
    }
    @Override
    protected void initData() {
        llNfcBefore.setVisibility(View.VISIBLE);
        llNfcAfter.setVisibility(View.GONE);
        if (isOpenNFC(NfcActivity.this)){
            initNFC();
        }else {
            XToast.error(NfcActivity.this, R.string.open_nfc).show();
//            ToastTool.shortToast(NfcActivity.this,"請開啟NFC功能");
        }
    }


    @OnClick(R2.id.tb_return)
    public void onViewClicked() {
        ActivityTool.getInstance().popOneActivity(this);
    }

    @Override
    protected void setListener() {

    }

    /**
     * NFC Functions
     */
    private void initNFC() {
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        try {
            ndef.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
         //   CrashHandler.saveExceptionInfoToFile(ReadNFCActivity.this,ReadNFCActivity.class.getSimpleName(),"initNFC()",e);
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[] { ndef, tag};
        mTechLists = new String[][] { new String[] { MifareClassic.class.getName() } };
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        resolveIntent(intent);
    }

    private void resolveIntent(Intent intent) {
        PIN = NFCTool.getTagId(intent);
        if (PIN != null && !PIN.equals("")) {
            llNfcBefore.setVisibility(View.GONE);
            llNfcAfter.setVisibility(View.VISIBLE);
//            if (PIN.substring(0,1).equals("0")) {
//                PIN = PIN.substring(1,PIN.length());
//            }
            Intent back = getIntent();
            back.putExtra("NFC_PIN",PIN);
            setResult(Activity.RESULT_OK, back);//返回页面1
            ActivityTool.getInstance().popOneActivity(this);
        }
    }

    private boolean isOpenNFC(Context context){
        boolean bRet=false;
        if(context==null)
            return bRet;
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(context);
        if (adapter != null) {
            bRet=true;
        }
        return bRet;
    }

    @Override
    protected void onResume() {
        try {
            if (nfcAdapter != null) {
                nfcAdapter.enableForegroundDispatch(NfcActivity.this, mPendingIntent, mFilters, mTechLists);
            }
        } catch (Exception e) {
            //CrashHandler.saveExceptionInfoToFile(ReadNFCActivity.this,"ReadNFCActivity","onResume",e);
            e.printStackTrace();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        try {
            if (nfcAdapter != null) {
                nfcAdapter.disableForegroundDispatch(NfcActivity.this);
            }
        } catch (Exception e) {
            Log.e("ReadNFCActivity", e.getMessage());
        }
        super.onPause();
    }

}
